package com.nulldev.util.encoding;

import com.nulldev.util.io.ZipUtils;
import com.nulldev.util.other.ByteUtils;
import com.nulldev.util.other.CharsetUtil;

public class Compressor {

	public static String compress(final String input) {
		String out = input;
		final String zipMethod = ZipUtils.zipS16(input);
		final String byteArray = ByteUtils.toHEX(ZipUtils.compress(input.getBytes(CharsetUtil.getCharset())));
		
		try {
			final String unzip = ZipUtils.unzipS16(zipMethod);
			if (unzip.equals(input) && unzip.length() <= input.length()) {
				out = zipMethod;
			}
		}catch (Exception e) {
		}
		
		try {
			final String ubyte = new String(ZipUtils.decompress(ByteUtils.fromHEX(byteArray)));
			if (ubyte.equals(input) && ubyte.length() <= input.length()) {
				out = byteArray;
			}
		}catch (Exception e) {
		}
		
		return out;
	}
}
